/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: xiaotutu
 * Date: 2024-12-14
 * Time: 23:11
 */
import sun.reflect.generics.tree.Tree;


public class BinarySearchTree {

    static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    public TreeNode root;//根节点

    /**
     * 找到val值  返回节点的地址
     * O(logn)
     * @param val
     * @return
     */
    public TreeNode search(int val) {
        TreeNode cur = root;
        while (cur != null) {
            if (cur.val > val) {
                cur = cur.left;
            }else if (cur.val < val) {
                cur = cur.right;
            }
            return cur;
        }
        return null;
    }

    /**
     * 插入节点值为key
     * @param key
     * @return
     */
    public boolean insert(int key) {
        TreeNode node = new TreeNode(key);

        if (root == null) {
            root = node;
            return true;
        }

        TreeNode cur = root;
        TreeNode parent = null;
        while (cur != null) {
            if (cur.val < key) {
                parent = cur;
                cur = cur.right;

            }else if (cur.val > key) {
                parent = cur;
                cur = cur.left;
            }else {
                // 一样的值是不能插入的
                return false;
            }
        }
        if (parent.val > key) {
            parent.left = node;
        }
        if (parent.val < key) {
            parent.right = node;

        }
        return true;
    }

    /**
     * 删除节点key
     * @param key
     */
    public void removeNode(int key) {
        TreeNode cur = root;
        TreeNode parent = null;
        while (cur != null) {
            if (cur.val < key) {
                parent = cur;
                cur = cur.right;
            }else if (cur.val > key) {
                parent = cur;
                cur = cur.left;
            }else {
                // 找到节点了, 开始删除.
                remove(cur, parent);
                return;
            }
        }
    }
    /**
     * 删除cur这个节点
     * @param cur 要删除的节点
     * @param parent 要删除的节点的父节点
     */
    private void remove(TreeNode cur, TreeNode parent) {

        if(cur.left == null) {
            if(cur == root) {
                root = cur.right;
            }else if(cur == parent.left) {
                parent.left = cur.right;
            }else {
                parent.right = cur.right;
            }
        }else if(cur.right == null) {
            if (cur == root) {
                root = cur.left;
            } else if (parent.left == cur) {
                parent.left = cur.left;
            } else {
                parent.right = cur.left;
            }
        }else {
            // cur的左右两边都不为null
            TreeNode target = cur.right;
            TreeNode targetParent = cur;
            while (target.left != null) {
                targetParent = target;
                target = target.left;
            }
            cur.val = targetParent.val;
            if (targetParent.left == target) {
                targetParent.left = target.right;
            }else {
                targetParent.right = target.right;
            }
        }


    }


}
